
#include <Unit.h>

using namespace std;

Unit::Unit(unsigned int ammount, const char* pUnitName):ammountInBaseUnit_m(ammount), pUnitName_m(pUnitName)
{
}

bool Unit::operator==( const Unit &unit ) const
{
	return ( (ammountInBaseUnit_m == unit.ammountInBaseUnit_m) && ( *pUnitName_m == *unit.pUnitName_m ) )? true:false;
}

unsigned int Unit::convertToBaseUnit(unsigned int ammount ) const
{
	return ammountInBaseUnit_m*ammount;
}

void Unit::stripUnit( unsigned int &ammountInThisUnit, unsigned int &remainingAmmount) const
{
	ammountInThisUnit = remainingAmmount / ammountInBaseUnit_m;
	remainingAmmount %= ammountInBaseUnit_m;	
}

void Unit::toString( ostream &os, unsigned int &ammountInThisUnit, unsigned int &remainingAmmount) const
{
	if( ammountInThisUnit > 0 )
	{
		os<<ammountInThisUnit<<" "<<pUnitName_m;

		if( remainingAmmount > 0 )
		{
			os<<" ";
		}
	}
}


void Unit::formatThisUnit( ostream &os, unsigned int &remainingAmmount) const
{
	unsigned int ammountInThisUnit;

	stripUnit( ammountInThisUnit, remainingAmmount);
	toString( os, ammountInThisUnit, remainingAmmount);

}


